package str;

import java.io.UnsupportedEncodingException;

import constant.StringConstants;

/**
 * <BR>
 *
 *
 * VERSION        NAME        DATE        REASON
 * ----------------------------------------------------------------------------
 * 1.00           Brian       Mar 10, 2012  Create
 * @author brian
 * @version 1.00
 */
public class StringUtil {

    /**
     * 字符串结合。<BR>
     * 结合用字符"-",需要结合字符["a","b"] → "a-b".<BR>
     *
     * @param c 结合用字符
     * @param items 需要结合字符串
     * @return 结合后字符
     */
    public static String combineItems(String c, String... items) {

        StringBuilder sb = new StringBuilder();
        int length = items.length;
        for (int i = 0; i < length; i++) {
            String s = items[i];
            if (i == length - 1) {
                sb.append(s);
                break;
            }
            if (c != null) {
                sb.append(s).append(c);
            } else {
                sb.append(s);
            }
        }
        return sb.toString();
    }

    /**
     * 使用StringBuilder进行字符连结.<BR>
     * 
     * @param items
     * @return 连结后字符
     */
    public static String appendItems(String... items) {

        return combineItems(null, items);
    }

    /**
     * 删除字符串中半角空格.<BR>
     * 
     * @param s
     * @return String
     */
    public static String trimAll(String s) {

        return s.replaceAll(StringConstants.HALF_SPACE, StringConstants.EMPTY);
    }

    /**
     * 删除字符串中间多余半角空格，只留一个半角空格.<BR>
     *
     * @param s
     * @return String
     */
    public static String middleTrim(String s){

        // TODO 请完成
        return s;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {

        System.out.println(trimAll("   ddd  ddd   fff  ggg  eee   "));
        System.out.println(" ".getBytes("utf-8").length);
        System.out.println(" ".getBytes("utf-16").length);
        System.out.println(" ".getBytes("gbk").length);
        System.out.println(" ".getBytes("windows-31j").length);
    }
}
